﻿using AutoMapper;
using MediatR;
using ProductionManagement.Application.Command.MaterialCommand;
using ProductionManagement.Domain.Dtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;
using ItemSubpieces = ProductionManagement.Domain.ProductionDomain.ItemSubpieces;

namespace ProductionManagement.Application.CommandHandler.MaterialCommandHandlers
{
    public class MaterialUpdCommandHandlers : IRequestHandler<MaterialUpdCommand, int>
    {
        private readonly IMaterialRepository _materialRepository;
        private readonly IItemSubpiecesRepository _itemSubpiecesRepository;
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public MaterialUpdCommandHandlers(IMaterialRepository materialRepository,
          IItemSubpiecesRepository itemSubpiecesRepository,
            ISqlSugarClient db,
            IMapper mapper)
        {
            _materialRepository = materialRepository;
            _itemSubpiecesRepository = itemSubpiecesRepository; 
            this.db = db;
            this.mapper = mapper;
        }


        public async Task<int> Handle(MaterialUpdCommand request, CancellationToken cancellationToken)
        {
            // 开启事务
            db.Ado.BeginTran();
            try
            {
                var material = mapper.Map<Material>(request.UpdDtos);
                int Item = await _materialRepository.UpdAsync(material);
                foreach (var subpiece in request.UpdDtos.ItemSubpiecesList)
                {
                    ItemSubpieces items = new ItemSubpieces
                    {
                        ItemId= subpiece.itemSubpieceId,
                        INumber=subpiece.INumber,
                        IProductPersonas=subpiece.IProductPersonas,
                        IProcess=subpiece.IProcess,
                        IWearRate=subpiece.IWearRate,
                        IPickingMethod=subpiece.IPickingMethod,
                        MaterialNode= request.UpdDtos.MaterialNode,
                        IMRemark=subpiece.IMRemark,
                        PName=subpiece.PName,   
                        PSpec=subpiece.PSpec,
                        PUnit=subpiece.PUnit,
                        ProductRatio=subpiece.ProductRatio,
                        PNumber=subpiece.PNumber,
                        AddName =request.UpdDtos.AddName,
                        AddTime=request.UpdDtos.AddTime,
                        UpdName = request.UpdDtos.UpdName,
                        UpdTime = request.UpdDtos.UpdTime,
                        Office_Id =request.UpdDtos.Office_Id,
                        User_Id=request.UpdDtos.User_Id
                    };
                    await _itemSubpiecesRepository.UpdAsync(items);

                }
                // 提交事务
                db.Ado.CommitTran();

                return 1; 


            }
            catch (Exception ex)
            {
                // 捕获异常并回滚事务
                db.Ado.RollbackTran();
                // 可以记录日志以便调试
                throw new Exception("事务失败，已回滚所有操作", ex);
            }
        }
    }
}
